
package panels;

import database.COINCOMOComponentManager;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import main.GlobalMethods;

/**
 *
 * @author Raed Shomali
 */
public class COPSEMOParametersPanel extends JPanel implements ChangeListener
{
    UpdateCOPSEMOGraph update = null ;
            
    // Spinners Models (Value , Min , Max , Step)
    SpinnerModel spinnerModel1 = new SpinnerNumberModel( 6.0 , 2.0 , 15.0 , 1 ) ;
    SpinnerModel spinnerModel2 = new SpinnerNumberModel( 24.0 , 20.0 , 28.0 , 1 ) ;
    SpinnerModel spinnerModel3 = new SpinnerNumberModel( 76.0 , 72.0 , 80.0 , 1 ) ;
    SpinnerModel spinnerModel4 = new SpinnerNumberModel( 12.0 , 0.0 , 20.0 , 1 ) ;
    SpinnerModel spinnerModel5 = new SpinnerNumberModel( 12.5 , 2.0 , 30.0 , 1 ) ;
    SpinnerModel spinnerModel6 = new SpinnerNumberModel( 37.5 , 33.0 , 42.0 , 1 ) ;
    SpinnerModel spinnerModel7 = new SpinnerNumberModel( 62.5 , 58.0 , 67.0 , 1 ) ;
    SpinnerModel spinnerModel8 = new SpinnerNumberModel( 12.5 , 0.0 , 20.0 , 1 ) ;
    
    // Spinners
    JSpinner inceptionEffortSpinner = new JSpinner( spinnerModel1 ) ;
    JSpinner elaborationEffortSpinner = new JSpinner( spinnerModel2 ) ;
    JSpinner constructionEffortSpinner = new JSpinner( spinnerModel3 ) ;
    JSpinner transitionEffortSpinner = new JSpinner( spinnerModel4 ) ;
    JSpinner inceptionScheduleSpinner = new JSpinner( spinnerModel5 ) ;
    JSpinner elaborationScheduleSpinner = new JSpinner( spinnerModel6 ) ;
    JSpinner constructionScheduleSpinner = new JSpinner( spinnerModel7 ) ;
    JSpinner transitionScheduleSpinner = new JSpinner( spinnerModel8 ) ;
    
    // Labels
    JLabel inceptionLabel = new JLabel( "Inception" ) ;
    JLabel elaborationLabel = new JLabel( "Elaboration" ) ;
    JLabel constructionLabel = new JLabel( "Construction" ) ;
    JLabel transitionLabel = new JLabel( "Transition" ) ;
    JLabel totalECLabel = new JLabel( "Total E&C" ) ;
    JLabel totalLabel = new JLabel( "Total" ) ;
    
    JLabel ECTotalEffort = new JLabel( "" ) ;
    JLabel totalEffort = new JLabel( "" ) ;
    
    JLabel ECTotalSchedule = new JLabel( "" ) ;
    JLabel totalSchedule = new JLabel( "" ) ;
    
    JLabel inceptionPMLabel = new JLabel( "19" ) ;
    JLabel elaborationPMLabel = new JLabel( "20" ) ;
    JLabel constructionPMLabel = new JLabel( "21" ) ;
    JLabel transitionPMLabel = new JLabel( "22" ) ;
    JLabel ECTotalPMLabel = new JLabel( "23" ) ;
    JLabel totalPMLabel = new JLabel( "24" ) ;
    JLabel inceptionMLabel = new JLabel( "25" ) ;
    JLabel elaborationScheduleLabel = new JLabel( "26" ) ;
    JLabel constructionMLabel = new JLabel( "27" ) ;
    JLabel transitionMLabel = new JLabel( "28" ) ;
    JLabel ECTotalMLabel = new JLabel( "29" ) ;
    JLabel totalScheduleMLabel = new JLabel( "30" ) ;
    JLabel inceptionPLabel = new JLabel( "31" ) ;
    JLabel elaborationPLabel = new JLabel( "32" ) ;
    JLabel constructionPLabel = new JLabel( "33" ) ;
    JLabel transitionPLabel = new JLabel( "34" ) ;
    JLabel ECTotalPLabel = new JLabel( "35" ) ;
    JLabel totalPLabel = new JLabel( "36" ) ;
    
    JLabel effortLabel = new JLabel( "Effort %" ) ;
    JLabel scheduleLabel = new JLabel( "Schedule %" ) ;
    JLabel PMLabel = new JLabel( "PM" ) ;
    JLabel MLabel = new JLabel( "M" ) ;
    JLabel PLabel = new JLabel( "PM / M" ) ;
    
    /**
     * Constructor
     */
    public COPSEMOParametersPanel()
    {
        // Spinners
        inceptionEffortSpinner.addChangeListener( this );
        transitionEffortSpinner.addChangeListener( this );
        inceptionScheduleSpinner.addChangeListener( this );
        transitionScheduleSpinner.addChangeListener( this );
        elaborationEffortSpinner.addChangeListener( this );
        constructionEffortSpinner.addChangeListener( this );
        elaborationScheduleSpinner.addChangeListener( this );
        constructionScheduleSpinner.addChangeListener( this );
        
        // Labels
        inceptionPMLabel.setHorizontalAlignment( JLabel.CENTER );
        elaborationPMLabel.setHorizontalAlignment( JLabel.CENTER );
        constructionPMLabel.setHorizontalAlignment( JLabel.CENTER );
        transitionPMLabel.setHorizontalAlignment( JLabel.CENTER );
        ECTotalPMLabel.setHorizontalAlignment( JLabel.CENTER );
        totalPMLabel.setHorizontalAlignment( JLabel.CENTER );
        inceptionMLabel.setHorizontalAlignment( JLabel.CENTER );
        elaborationScheduleLabel.setHorizontalAlignment( JLabel.CENTER );
        constructionMLabel.setHorizontalAlignment( JLabel.CENTER );
        transitionMLabel.setHorizontalAlignment( JLabel.CENTER );
        ECTotalMLabel.setHorizontalAlignment( JLabel.CENTER );
        totalScheduleMLabel.setHorizontalAlignment( JLabel.CENTER );
        inceptionPLabel.setHorizontalAlignment( JLabel.CENTER );
        elaborationPLabel.setHorizontalAlignment( JLabel.CENTER );
        constructionPLabel.setHorizontalAlignment( JLabel.CENTER );
        transitionPLabel.setHorizontalAlignment( JLabel.CENTER );
        ECTotalPLabel.setHorizontalAlignment( JLabel.CENTER );
        totalPLabel.setHorizontalAlignment( JLabel.CENTER );
        
        ECTotalEffort.setHorizontalAlignment( JLabel.CENTER );
        ECTotalSchedule.setHorizontalAlignment( JLabel.CENTER );
        totalEffort.setHorizontalAlignment( JLabel.CENTER );
        totalSchedule.setHorizontalAlignment( JLabel.CENTER );
        
        PMLabel.setHorizontalAlignment( JLabel.CENTER );
        MLabel.setHorizontalAlignment( JLabel.CENTER );
        PLabel.setHorizontalAlignment( JLabel.CENTER );
        
        
        // GUI
        this.setLayout( null );
        
        this.add( inceptionEffortSpinner  ) ;
        this.add( elaborationEffortSpinner  ) ;
        this.add( constructionEffortSpinner  ) ;
        this.add( transitionEffortSpinner  ) ;
        this.add( inceptionScheduleSpinner  ) ;
        this.add( elaborationScheduleSpinner  ) ;
        this.add( constructionScheduleSpinner  ) ;
        this.add( transitionScheduleSpinner  ) ;
        
        this.add( inceptionLabel  ) ;
        this.add( elaborationLabel  ) ;
        this.add( constructionLabel  ) ;
        this.add( transitionLabel  ) ;
        this.add( totalECLabel  ) ;
        this.add( totalLabel  ) ;
        
        this.add( ECTotalEffort  ) ;
        this.add( totalEffort  ) ;
        
        this.add( ECTotalSchedule  ) ;
        this.add( totalSchedule  ) ;
        this.add( inceptionPMLabel  ) ;
        this.add( elaborationPMLabel  ) ;
        this.add( constructionPMLabel  ) ;
        this.add( transitionPMLabel  ) ;
        this.add( ECTotalPMLabel  ) ;
        this.add( totalPMLabel  ) ;
        this.add( inceptionMLabel  ) ;
        this.add( elaborationScheduleLabel  ) ;
        this.add( constructionMLabel  ) ;
        this.add( transitionMLabel  ) ;
        this.add( ECTotalMLabel  ) ;
        this.add( totalScheduleMLabel  ) ;
        this.add( inceptionPLabel  ) ;
        this.add( elaborationPLabel  ) ;
        this.add( constructionPLabel  ) ;
        this.add( transitionPLabel  ) ;
        this.add( ECTotalPLabel  ) ;
        this.add( totalPLabel  ) ;
        
        this.add( effortLabel  ) ;
        this.add( scheduleLabel  ) ;
        
        this.add( PMLabel  ) ;
        this.add( MLabel  ) ;
        this.add( PLabel  ) ;
        
        inceptionLabel.setBounds( 100 , 20 , 90 , 22 ) ;
        elaborationLabel.setBounds( 190 , 20 , 90 , 22 ) ;
        constructionLabel.setBounds( 280 , 20 , 90 , 22 ) ;
        transitionLabel.setBounds( 370 , 20 , 90 , 22 ) ;
        totalECLabel.setBounds( 460 , 20 , 90 , 22 ) ;
        totalLabel.setBounds( 550 , 20 , 90 , 22 ) ;
        
        inceptionEffortSpinner.setBounds( 100 , 45 , 75 , 22 ) ;
        elaborationEffortSpinner.setBounds( 190 , 45 , 75 , 22 ) ;
        constructionEffortSpinner.setBounds( 280 , 45 , 75 , 22 ) ;
        transitionEffortSpinner.setBounds( 370 , 45 , 75 , 22 ) ;
        ECTotalEffort.setBounds( 460 , 45 , 60 , 22 ) ;
        totalEffort.setBounds( 540 , 45 , 50 , 22 ) ;
        
        inceptionScheduleSpinner.setBounds( 100 , 70 , 75 , 22 ) ;
        elaborationScheduleSpinner.setBounds( 190 , 70 , 75 , 22 ) ;
        constructionScheduleSpinner.setBounds( 280 , 70 , 75 , 22 ) ;
        transitionScheduleSpinner.setBounds( 370 , 70 , 75 , 22 ) ;
        ECTotalSchedule.setBounds( 460 , 70 , 60 , 22 ) ;
        totalSchedule.setBounds( 540 , 70 , 50 , 22 ) ;
        
        inceptionPMLabel.setBounds( 100 , 95 , 70 , 22 ) ;
        elaborationPMLabel.setBounds( 190 , 95 , 70 , 22 ) ;
        constructionPMLabel.setBounds( 280 , 95 , 70 , 22 ) ;
        transitionPMLabel.setBounds( 370 , 95 , 70 , 22 ) ;
        ECTotalPMLabel.setBounds( 460 , 95 , 60 , 22 ) ;
        totalPMLabel.setBounds( 540 , 95 , 50 , 22 ) ;
        
        inceptionMLabel.setBounds( 100 , 120 , 70 , 22 ) ;
        elaborationScheduleLabel.setBounds( 190 , 120 , 70 , 22 ) ;
        constructionMLabel.setBounds( 280 , 120 , 70 , 22 ) ;
        transitionMLabel.setBounds( 370 , 120 , 70 , 22 ) ;
        ECTotalMLabel.setBounds( 460 , 120 , 60 , 22 ) ;
        totalScheduleMLabel.setBounds( 540 , 120 , 50 , 22 ) ;
        
        inceptionPLabel.setBounds( 100 , 145 , 70 , 22 ) ;
        elaborationPLabel.setBounds( 190 , 145 , 70 , 22 ) ;
        constructionPLabel.setBounds( 280 , 145 , 70 , 22 ) ;
        transitionPLabel.setBounds( 370 , 145 , 70 , 22 ) ;
        ECTotalPLabel.setBounds( 460 , 145 , 60 , 22 ) ;
        totalPLabel.setBounds( 540 , 145 , 50 , 22 ) ;
        
        effortLabel.setBounds( 10 , 45 , 90 , 22 ) ;
        scheduleLabel.setBounds( 10 , 70 , 90 , 22 ) ;
        PMLabel.setBounds( 10 , 95 , 50 , 22 ) ;
        MLabel.setBounds( 10 , 120 , 50 , 22 ) ;
        PLabel.setBounds( 10 , 145 , 50 , 22 ) ;
    }

    public void stateChanged( ChangeEvent e )
    {
        // Update Other Spinners As Current Spinner Being Updated ..
        
        if( e.getSource() == elaborationEffortSpinner )
        {
            constructionEffortSpinner.setValue( 100.0 - Double.parseDouble( elaborationEffortSpinner.getValue() + "" ) );
            
            COPSEMOPanel.component.setElaborationEffortPercentage( Double.parseDouble( elaborationEffortSpinner.getValue() + "" ) );
            COPSEMOPanel.component.setConstructionEffortPercentage( 100.0 - Double.parseDouble( elaborationEffortSpinner.getValue() + "" ) );
        }
        else if( e.getSource() == constructionEffortSpinner )
        {
            elaborationEffortSpinner.setValue( 100.0 - Double.parseDouble( constructionEffortSpinner.getValue() + "" ) );
            
            COPSEMOPanel.component.setElaborationEffortPercentage( Double.parseDouble( elaborationEffortSpinner.getValue() + "" ) );
            COPSEMOPanel.component.setConstructionEffortPercentage( 100.0 - Double.parseDouble( elaborationEffortSpinner.getValue() + "" ) );
        }
        else if( e.getSource() == elaborationScheduleSpinner )
        {
            constructionScheduleSpinner.setValue( 100.0 - Double.parseDouble( elaborationScheduleSpinner.getValue() + "" ) );
            
            COPSEMOPanel.component.setElaborationSchedulePercentage( Double.parseDouble( elaborationScheduleSpinner.getValue() + "" ) );
            COPSEMOPanel.component.setConstructionSchedulePercentage( 100.0 - Double.parseDouble( elaborationScheduleSpinner.getValue() + "" ) );
        }
        else if( e.getSource() == constructionScheduleSpinner )
        {
            elaborationScheduleSpinner.setValue( 100.0 - Double.parseDouble( constructionScheduleSpinner.getValue() + "" ) );
            
            COPSEMOPanel.component.setElaborationSchedulePercentage( Double.parseDouble( elaborationScheduleSpinner.getValue() + "" ) );
            COPSEMOPanel.component.setConstructionSchedulePercentage( 100.0 - Double.parseDouble( elaborationScheduleSpinner.getValue() + "" ) );
        }
        
        COPSEMOPanel.component.setInceptionEffortPercentage( Double.parseDouble( inceptionEffortSpinner.getValue() + "" ) );
        COPSEMOPanel.component.setInceptionSchedulePercentage( Double.parseDouble( inceptionScheduleSpinner.getValue() + "" ) );
        COPSEMOPanel.component.setTransitionEffortPercentage( Double.parseDouble( transitionEffortSpinner.getValue() + "" ) );
        COPSEMOPanel.component.setTransitionSchedulePercentage( Double.parseDouble( transitionScheduleSpinner.getValue() + "" ) );
        
        // Alreadt Exists ?
        if( update != null )
        {
            // Kill It
            update.interrupt();
        }
        
        // Create a New Thread
        update = new UpdateCOPSEMOGraph() ;
        update.start();
    }
    
    static class UpdateCOPSEMOGraph extends Thread
    {
        public UpdateCOPSEMOGraph()
        {
            // Lowest Priority ..
            // So the User Can Play With the System..
            this.setPriority( 1 );
        }
        
        @Override
        public void run()
        {           
            // Show the Loading Text ...
            COPSEMOPanel.graph.isLoading = true ;
            COPSEMOPanel.graph.repaint() ;
            GlobalMethods.updateStatusBar( "Loading COPSEMO ..." );
            
            // Update the Database ...
            COINCOMOComponentManager.updateComponent( COPSEMOPanel.component ) ;
            
            // Update the Graph's Parameters ..
            OverviewsAndGraphsPanel.copsemo.updateCOPSEMO( COPSEMOPanel.component );  
            
            // Show the Graph ..
            COPSEMOPanel.graph.isLoading = false ;
            COPSEMOPanel.graph.repaint() ;
            GlobalMethods.updateStatusBar( "COPSEMO Loaded." );
        }
    }
    
    /**
     * 
     * @param value sets the Inception's Effort's Spinner
     */
    public void setInceptionEffortPercentage( double value )
    {
        inceptionEffortSpinner.setValue( value );
    }
    
    /**
     * 
     * @param value sets the Inception's Schedule's Spinner
     */
    public void setInceptionSchedulePercentage( double value )
    {
        inceptionScheduleSpinner.setValue( value );
    }
    
    /**
     * 
     * @param value sets the Elaboration's Effort's Spinner
     */
    public void setElaborationEffortPercentage( double value )
    {
        elaborationEffortSpinner.setValue( value );
    }
    
    /**
     * 
     * @param value sets the Elaboration's Schedule's Spinner
     */
    public void setElaborationSchedulePercentage( double value )
    {
        elaborationScheduleSpinner.setValue( value );
    }
    
    /**
     * 
     * @param value sets the Construction's Effort's Spinner
     */
    public void setConstructionEffortPercentage( double value )
    {
        constructionEffortSpinner.setValue( value );
    }
    
    /**
     * 
     * @param value sets the Construction's Schedule's Spinner
     */
    public void setConstructionSchedulePercentage( double value )
    {
        constructionScheduleSpinner.setValue( value );
    }
    
    /**
     * 
     * @param value sets the Transition's Effort's Spinner
     */
    public void setTransitionEffortPercentage( double value )
    {
        transitionEffortSpinner.setValue( value );
    }
    
    /**
     * 
     * @param value sets the Transition's Schedule's Spinner
     */
    public void setTransitionSchedulePercentage( double value )
    {
        transitionScheduleSpinner.setValue( value );
    }
    
    /**
     * 
     * @param value sets the Inception's Effort's Label
     */
    public void setInceptionPM( String value )
    {
        inceptionPMLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets the Elaboration's Effort Label
     */
    public void setElaborationPM( String value )
    {
        elaborationPMLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets the Construction's Effort Label
     */
    public void setConstructionPM( String value )
    {
        constructionPMLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets the Transition's Effort Label
     */
    public void setTransitionPM( String value )
    {
        transitionPMLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets the EC Total's Effort Label
     */
    public void setECTotalPM( String value )
    {
        ECTotalPMLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets the Total Effort Label
     */
    public void setTotalPM( String value )
    {
        totalPMLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets the Inception's Schedule Label
     */
    public void setInceptionM( String value )
    {
        inceptionMLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets the Elaboration's Schedule's Label
     */
    public void setElaborationM( String value )
    {
        elaborationScheduleLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets the Construction's Schedule Label
     */
    public void setConstructionM( String value )
    {
        constructionMLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets the Transition's Schedule Label
     */
    public void setTransitionM( String value )
    {
        transitionMLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets EC Total's Schedule's Label
     */
    public void setECTotalM( String value )
    {
        ECTotalMLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets the Total Schedule Label
     */
    public void setTotalM( String value )
    {
        totalScheduleMLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets Inception Personnel Label
     */
    public void setInceptionP( String value )
    {
        inceptionPLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets elaboration Personnel Label
     */
    public void setElaborationP( String value )
    {
        elaborationPLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets Construction Personnel Label
     */
    public void setConstructionP( String value )
    {
        constructionPLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets Transition Personnel Label
     */
    public void setTransitionP( String value )
    {
        transitionPLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets EC Total Personnel Label
     */
    public void setECTotalP( String value )
    {
        ECTotalPLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets Total Personnel Label
     */
    public void setTotalP( String value )
    {
        totalPLabel.setText( value );
    }
    
    /**
     * 
     * @param value sets EC Total Effort Label
     */
    public void setECTotalEffort( String value )
    {
        ECTotalEffort.setText( value );
    }
    
    /**
     * 
     * @param value sets EC Total Schedule Label
     */
    public void setECTotalSchedule( String value )
    {
        ECTotalSchedule.setText( value );
    }
    
    /**
     * 
     * @param value sets Total Effort Label
     */
    public void setTotalEffort( String value )
    {
        totalEffort.setText( value );
    }
    
    /**
     * 
     * @param value sets Total Schedule Label
     */
    public void setTotalSchedule( String value )
    {
        totalSchedule.setText( value );
    }
}
